package bid.yanhj.yanhaijiang.fragmentpageradapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.SparseArray;

import java.util.List;

public class MainADapter extends FragmentPagerAdapter {
//    private List<Fragment> mFragments;

//    public MainADapter(FragmentManager fm, List<Fragment> fragments) {
//        super(fm);
//        mFragments = fragments;
//    }

    // 会走Fragment销毁的生命周期
    private SparseArray<Fragment> mFragment;

    public MainADapter(FragmentManager fm) {
        super(fm);
        mFragment = new SparseArray<>();
    }
    // 获取第几页

    /**
     * 构建屏幕 先从SparseArray 那
     * @param position
     * @return
     */
    @Override
    public Fragment getItem(int position) {
//        return mFragments.get(position);
//        return TextFragment.getTextFragment(String.valueOf(position));
        Fragment fragment = mFragment.get(position);
        if (fragment == null) {
            // 构建fragment 添加在mFragment集合
            fragment = TextFragment.getTextFragment(String.valueOf(position));
            mFragment.put(position, fragment);
        }
        return  fragment;
    }

    @Override
    public int getCount() {
//        return mFragments.size();
        return 20;
    }
}
